package com.moefor.clover.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.moefor.clover.model.User;
import com.moefor.clover.model.enums.CloverSessionKey;
import com.moefor.clover.service.CommunicationService;
import com.moefor.clover.util.MessagePackager;

@Controller
@RequestMapping(value = "/msg")
public class MessageController {
	@Resource
	private CommunicationService communicationService;
	
	@ResponseBody
	@RequestMapping(value = "/send.do")
	public MessagePackager sendMsg(HttpSession session,@RequestParam("receiver_id") Integer receiverId, @RequestParam("content") String content){
		User u = (User)session.getAttribute(CloverSessionKey.USER);
		MessagePackager msgPack = communicationService.sendMessage(u, receiverId, content);
		return msgPack;
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_all.do")
	public MessagePackager getAllMsg(HttpSession session){
		User u = (User)session.getAttribute(CloverSessionKey.USER);
		MessagePackager msgPack = communicationService.getMessageByReceiver(u.getId());		
		return msgPack;
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_unread.do")
	public MessagePackager getUnreadMsg(){
		return null;
	}
	
	@ResponseBody
	@RequestMapping(value = "/read_all.do")
	public MessagePackager readAllMsg(HttpSession session){
		User u = (User)session.getAttribute(CloverSessionKey.USER);
		System.out.println("enter");
		communicationService.readAllMessage(u.getId());
		return null;
	}
	
	
}